﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using ZHDJ.Core.Log;
using ZHDJ.Core.Util;
using ZHDJ.WinService.Job;
using ZHDJ.WinService.Util;

namespace ZHDJ.WinService
{
    partial class MainService : ServiceBase
    {
        public MainService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。

            try
            {
                LogHelper.Info("服务已启动");
                ScheduleHelper.AddJob<ScoreJob>(new ScoreJob());
                ScheduleHelper.AddJob<InitBranchRelationJob>(new InitBranchRelationJob());
                ScheduleHelper.AddJob<ExamUserSyncJob>(new ExamUserSyncJob());
                ScheduleHelper.AddJob<PartyVisitStatisticJob>(new PartyVisitStatisticJob());
                ScheduleHelper.AddJob<SmsSendJob>(new SmsSendJob());
                //ScheduleHelper.AddJob(new RunScheduledJob());
                foreach (var job in ScheduleHelper.JobBaseList)
                {
                    ExceptionUtil.CatchException(job.OnStart);
                }
                ScheduleHelper.Scheduler.Start();
            }
            catch (Exception ex)
            {

                LogHelper.Error(ex); 
            }
        }

        protected override void OnStop()
        {
            try
            {
                
                ScheduleHelper.Scheduler.Shutdown();
                //ScheduleHelper.Scheduler.Clear();
                foreach (var job in ScheduleHelper.JobBaseList)
                {
                    ExceptionUtil.CatchException(job.OnStop);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            LogHelper.Info("服务已停止");
        }
    }
}
